package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author littlebadfox
 * @date 2022-04-27-10:00
 * @desc
 */
@Component
public class Utils {
    //map转化为实体类对象
    public  <T> T parseMap2Object(Map<String, Object> paramMap, Class<T> cls) {
        return JSONObject.parseObject(JSONObject.toJSONString(paramMap), cls);
    }
    //list数据转化为list对象
    public <T> List<T> parseMaplist2ObjectList(List<Object> paramMap,Class<T> cls){
        List<T> list=new ArrayList<>();
        list= JSONArray.parseArray(JSONObject.toJSONString(paramMap),cls);
        return list;
    }
    //根据日期判断周几
    public String getWeek(String sdate) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] weeks = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Date parseDate = dateFormat.parse(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(parseDate);
        int week_index = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }
}
